# 1 "system.c"
# 1 "/home/diego/sodium/kernel//"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "system.c"
# 1 "../include/video.h" 1
# 13 "../include/video.h"
# 1 "../include/kernel/definiciones.h" 1
# 14 "../include/kernel/definiciones.h"
# 1 "../include/usr/tipos.h" 1
# 15 "../include/usr/tipos.h"
typedef unsigned int dword;
typedef unsigned short word;
typedef unsigned char byte;

typedef unsigned char u8;
typedef signed char s8;
typedef unsigned short u16;
typedef signed short s16;
typedef unsigned int u32;
typedef signed int s32;


typedef struct _u80 { unsigned int word[20]; } u80;
# 15 "../include/kernel/definiciones.h" 2
# 62 "../include/kernel/definiciones.h"
char stArgAux[128];
# 14 "../include/video.h" 2
# 1 "../include/kernel/libk/string.h" 1
# 9 "../include/kernel/libk/string.h"
inline unsigned char* ucpFnCopiarMemoria(unsigned char *ucpDestino,
      unsigned char *ucpOrigen,
      unsigned int uiTamanio);
inline unsigned char* ucpFnMemSetCero(unsigned char *ucpDirInicio,
                  unsigned int uiTamanio );

int iFnLongitudCadena (const char *cnstCadena);
int iFnCompararCadenas (const char *, const char *);
int iFnCompararCadenas2 (const char *, const char *);
int iFnCopiaCaracter (char *, const char *);
int iFnCopiaCadena (char *, const char *);

int iFnCopiaEntero (char *, const int);
int iFnCopiaFloat (char *, const float, const int);
int iFnCopiaWord (char *, const word);
int iFnCopiaByte (char *, const byte);
int iFnCopiaHexa (char *, const int);
int iFnCopiaWordHexa (char *, const word);
int iFnCopiaByteHexa (char *, const byte);
void vFnImprimirString (char *, const char *, ...);
int iFnLongitudCadena (const char *);

int iFnCopiaCadena2 (char *, const char *);
char * pstFnConcatenarCadena (char * , char * );
int iFnBuscarEnCadena (char * , char * ,int );
char * pstFnCadenaIzquierda (char * , int );
char * pstFnCadenaDerecha (char * , int );
int iFnEsNumero (char * stCadena);

void vFnStrLwr (char * stCadena);
void vFnStrUpr (char * stCadena);
# 15 "../include/video.h" 2
# 58 "../include/video.h"
typedef char* va_list;
# 100 "../include/video.h"
int iFnImprimirCadenas (int,const char*);




void vFnImprimirEntero (int,const int);




void vFnImprimirFloat (int,const double, const int);




void vFnImprimirHexa (int,const int);




void vFnImprimirWordHexa (int,const word);




void vFnImprimirByteHexa (int,const byte);





void vFnImprimirWin (int,const char*, ... );
void vFnImprimir (const char*, ... );
void vFnImprimirI (const char*, ... );

extern char cFnSysObtenerColor(int);
extern void vFnSysSetearColor(int,char);
extern void vFnSysSetearX(int,int);
extern void vFnSysSetearY(int,int);
extern void vFnClsWin(int);





void vFnImprimirContextSwitch(int color,int pid, char* nombre, int indiceGDT);
void vFnImprimirPrompt();
void vFnImprimirOk(int linea);
void vFnImprimirNOk(int linea);

void vFnImprimirVentana (int hVentana, const char *cnstCadena, ...);






int bTerminalActiva;

void vFnCambiarTerminal(void);
void vFnLog (const char *cnstCadena, ...);

void vFnLogScrollArriba();
void vFnLogScrollAbajo();

void vFnLogScrollMaximo();
void vFnLogScrollMinimo();
void vFnLogBorrar();
# 2 "system.c" 2

# 1 "../include/kernel/mem/mem_part_kernel.h" 1



void* ptrHeapBajoKernel;
long int liTamanioHeapBajoKernel;

void* ptrHeapAltoUsuario;
long int liTamanioHeapAltoUsuario;


long int liTamFijaMemBaja;
long int liTamFijaMemAlta;
long int liTamanioSolicitado;

int iModoMemoriaKernel;
char stTamanioMemFija[20];
int iTamanioMemFijaSeteado;
int iPosicionSetMemFija;
int iContinuar;
int iModoFijoUser;

void vFnInicializarHeapsKernel();
void vFnConfigurarParticiones();

void * pvFnReservarParticionUsuario(unsigned int);
void * pvFnReservarParticionKernel(unsigned int);
void * pvFnRedimensionarSegmento(void*, unsigned long);
void vFnLiberarParticion( void * );
unsigned int uiHeapKernelInicializado;
unsigned int uiKernelStack;
# 4 "system.c" 2
# 1 "../include/kernel/mem/mem_part_firstfit.h" 1
# 22 "../include/kernel/mem/mem_part_firstfit.h"
typedef struct {
        int piPorcentajesPartFija[100];
} t_Porcentaje;

typedef struct {
 int iUsado;
 long int liTam;
} t_Nodo;



void* ptrHeap;
t_Nodo* ptrActual;
long int liHeapEnd;
int iModo;
int iKernel;
int iModoFijo;

long int piDistribucionPartFija[100];
long int piPorcentajesPartFija[100];

long int liTamanioLiberado;
long int liTamHeap;
long int liTamFija;
long int liTamHeapFijoVariable;

void vFnCrearNodo(long int, int, long int);
int iInicializar();
void* pvFnMalloc(long int);
void* pvFnRealloc(long int, unsigned long);
int iFnFree(long int);
# 5 "system.c" 2
# 1 "../include/kernel/system.h" 1
# 22 "../include/kernel/system.h"
unsigned long int uliClockTick;





typedef struct
{
    unsigned int :16;
    unsigned int TSSSegmentSelector :16;
    unsigned int :8 ;
    unsigned int Type :5;
    unsigned int DPL :2;
    unsigned int P :1;
    unsigned int :16;
} __attribute__((packed)) t_TaskGate;





typedef struct
{
    unsigned int Offset1 :16;
    unsigned int SegmentSelector :16;
    unsigned int :5 ;
    unsigned int Type :8;
    unsigned int DPL :2;
    unsigned int P :1;
    unsigned int Offset2 :16;
} __attribute__((packed)) t_InterruptGate;




typedef struct
{
    unsigned int Offset1 :16;
    unsigned int SegmentSelector :16;
    unsigned int :5 ;
    unsigned int Type :8;
    unsigned int DPL :2;
    unsigned int P :1;
    unsigned int Offset2 :16;
} __attribute__((packed)) t_TrapGate;




typedef union
{
    t_TaskGate TaskGate;
    t_InterruptGate InterruptGate;
    t_TrapGate TrapGate;
}stuIDTDescriptor;




typedef struct
{
    stuIDTDescriptor IDTDescriptor[256];
} stuIDT;


typedef struct
{
    dword dwLimite :16;
    dword dwBase :32;
} t_IDTRegister;

stuIDT *pstuIDT;
unsigned char ucFnObtenerScanCode();
void vFnIniciarTeclado();
void vFnIniciarExcepciones();
void vFnIniciarIDT();
void vFnHandlerGenerico();
void vFnHandlerTimer();
void vFnPlanificador();
void vFnHandlerTeclado();
void* pvFnKernelStackAdmin();


void vFnHandlerDisquette();

long vFnHandlerSyscall( long eax,
   long ebx,
   long ecx,
   long edx );

void vFnLTR(unsigned int uiSelector);
unsigned int uiFnSTR(void);
# 6 "system.c" 2
# 1 "../include/kernel/sched.h" 1
# 15 "../include/kernel/sched.h"
typedef struct
{
 int sched_priority;
}sched_param;


unsigned long int uliTimeSlice;


unsigned long int uliQuantum;

unsigned long int uliBTSQ;


static int staiN = 0;

static int staiProcesoAnterior = -1;


int iTareaNula;

void vFnPlanificadorFIFO();
void vFnPlanificadorRR();
void vFnPlanificadorBTS();
# 7 "system.c" 2
# 1 "../include/kernel/system_asm.h" 1
# 10 "../include/kernel/system_asm.h"
void vFnIniciarIDT_Asm(const word wLimit, const void *pvBase);
void vFnRemapearPIC_Asm();
void vFnIniciarTimer_Asm();
void vFnHandlerGenerico_Asm();
void vFnHandlerTimer_Asm();
void vFnHandlerTeclado_Asm();
void vFnExcepcionCPU7_Asm();
void vFnExcepcionCPU13_Asm();
void vFnExcepcionCPU16_Asm();
void vFnHandlerSyscall_Asm();
void vFnHandlerCallgate_Asm();
void vFnHandlerDisquette_Asm();
# 8 "system.c" 2
# 1 "../include/kernel/puertos.h" 1
# 26 "../include/kernel/puertos.h"
byte inb(word);
# 38 "../include/kernel/puertos.h"
word inw(word);
# 50 "../include/kernel/puertos.h"
void outb(const byte valor, const word puerto);
# 61 "../include/kernel/puertos.h"
void outw(const word valor, const word puerto);
# 9 "system.c" 2
# 1 "../include/kernel/pcb.h" 1







# 1 "../include/kernel/shm.h" 1
# 18 "../include/kernel/shm.h"
typedef unsigned int size_t;
typedef int key_t;
typedef int bool;


typedef struct _stuMemoriasAtachadas_
{
 bool utilizada;
 int posicionEnShMem;
 int posicionEnAttach;
}stuMemoriasAtachadas;


typedef struct _attach_
{
 int pid;
 void * ptrShAddr;

}attach;


typedef struct _shMem_
{
 key_t key;
 unsigned long int tamanio;
 attach procAtt[5];
 bool declarada;
}shMem;

shMem memoriasCompartidas[5];




int iFnShmGet(key_t key, size_t size);


int iFnShmAt(int shmid, void * shmAddr);


int iFnShmDt(int shmid);

int iFnShmDtProc(int shmid, unsigned int uiPid);
int iFnShmDtAllProc(unsigned int uiPid);

unsigned long ulFnMaxDirShmProc(unsigned int uiPid);

void vFnInicializarShms();
void vFnVerShm();




void vFnCopiarVariablesCompartidas();



void vFnCopiarVariable(void * dest, void * src, size_t tamanio);
# 9 "../include/kernel/pcb.h" 2
# 1 "../include/kernel/tiempo.h" 1
# 53 "../include/kernel/tiempo.h"
typedef unsigned long int time_t;
typedef unsigned long int clock_t;





typedef struct{

        clock_t tms_utime;


        clock_t tms_stime;


        clock_t tms_cutime;


        clock_t tms_cstime;
} tms;





typedef struct{

        int tz_minuteswest;


        int tz_dsttime;
} timezone;





typedef struct{

        unsigned long int tv_sec;


        unsigned long int tv_usec;
} timeval;





typedef struct{

        timeval it_interval;


        timeval it_value;
} itimerval;





typedef struct{

        int modes;


 long offset;


 long freq;


 long maxerror;


 long esterror;


 int status;


 long constant;


 long precision;


 long tolerance;


 timeval time;


 long tick;
} timex;





typedef struct{

        long int tv_sec;


        long int tv_nsec;
} timespec;
# 10 "../include/kernel/pcb.h" 2
# 27 "../include/kernel/pcb.h"
typedef struct {
 unsigned char ident[16];
 unsigned short type;
 unsigned short machine;
 unsigned int version;
 unsigned int entry;
 unsigned int phoff;
 unsigned int sphoff;
 unsigned int flags;
 unsigned short ehsize;
 unsigned short phentsize;
 unsigned short phnum;
 unsigned short shentsize;
 unsigned short shnum;
 unsigned short shstrndx;
} elfHeader;

typedef struct {
 unsigned int name;
 unsigned int type;
 unsigned int flags;
 unsigned int addr;
 unsigned int offset;
 unsigned int size;
 unsigned int link;
 unsigned int info;
 unsigned int addralign;
 unsigned int entsize;
}SHeader;

typedef struct {
        unsigned int type;
 unsigned int offset;
 unsigned int vaddr;
        unsigned int paddr;
        unsigned int filesz;
        unsigned int memsz;
        unsigned int flags;
        unsigned int align;
} PHeader;






typedef struct {
   elfHeader elfHeaderData;
   SHeader SHeaderData;
   PHeader PHeaderData;
   unsigned char ucbuffer[4];
} stuHeaderELF;





typedef struct {
   int iEstado;
   long int liTamanio;
} stuEsperarMemoria;
# 96 "../include/kernel/pcb.h"
typedef struct _stuPCB_{
  unsigned long int ulId;
  unsigned int uiIndiceGDT_CS;
  unsigned int uiIndiceGDT_DS;
  unsigned int uiIndiceGDT_TSS;
  unsigned int uiIndiceGDT_SS0;
  void (* vFnFuncion) ();

  unsigned long ulParentId;
  unsigned long ulUsuarioId;
  int iPrioridad;
  int iEstado;
  unsigned long lNHijos;
  int iExitStatus;
  unsigned long ulLugarTSS;
  char stNombre[25];
  unsigned int uiTamProc;
  struct stuTablaPagina * pstuTablaPaginacion;
  unsigned int uiDirBase,

        uiLimite;
  stuMemoriasAtachadas memoriasAtachadas[5];


  unsigned long ulTiempoEspera;
  unsigned long ulTiempoServicio;



  struct stuPCB* pstuPcbSiguiente;

  tms stuTmsTiemposProceso;

  itimerval timers[3];

  long lNanosleep;

  unsigned int *puRestoDelNanosleep;

    long lPidTracer;

    unsigned int uiTamanioTexto;
    unsigned int uiTamanioDatosInicializados;
    unsigned int uiTamanioStack;

    unsigned int uiTamanioOverhead;

    unsigned int uiEsperaTeclado;

    stuHeaderELF stuCabeceraELF;
    int iModoParticion;
    stuEsperarMemoria stuEsperaMemoria;
    unsigned int uiBaseSs0;
}stuPCB;
# 10 "system.c" 2
# 1 "../include/kernel/gdt.h" 1
# 32 "../include/kernel/gdt.h"
stuPCB pstuPCB[50];

int iCOLA_FLOPPY_ES[50];

unsigned int uiCOLA_FLOPPY_ES_INDEX_INI;
unsigned int uiCOLA_FLOPPY_ES_INDEX_FIN;
unsigned int uiPROC_FLOPPY;
unsigned int uiFIN_FLOPPY;
unsigned int uiCiclo;

unsigned int uiTamHeap;
# 63 "../include/kernel/gdt.h"
typedef struct {
  unsigned short usLimiteBajo;
  unsigned short usBaseBajo;
  unsigned char ucBaseMedio;







 unsigned char ucAcesso;

  unsigned int bitLimiteAlto:4;
# 89 "../include/kernel/gdt.h"
 unsigned int bitGranularidad:4;

  unsigned char usBaseAlto;

} __attribute__((packed)) stuGDTDescriptor;






typedef struct {
   stuGDTDescriptor stuGdtDescriptorDescs[8192];
} stuEstructuraGdt;



stuEstructuraGdt *pstuTablaGdt;






typedef struct {
  unsigned int control;
  unsigned int status;
  unsigned int tag;
  unsigned int ip;
  unsigned int cs;
  unsigned int dp;
  unsigned int ds;
  u80 st[8];
} __attribute__((packed)) stuFpu;
# 131 "../include/kernel/gdt.h"
typedef struct {
  unsigned int uiEnlace;
  unsigned int esp0;
  unsigned int ss0;
  unsigned int esp1;
  unsigned int ss1;
  unsigned int esp2;
  unsigned int ss2;
  unsigned int cr3;
  unsigned int eip;
  unsigned int uiEBandera;
  unsigned int eax;
  unsigned int ecx;
  unsigned int edx;
  unsigned int ebx;
  unsigned int esp;
  unsigned int ebp;
  unsigned int esi;
  unsigned int edi;
  unsigned int es;
  unsigned int cs;
  unsigned int ss;
  unsigned int ds;
  unsigned int fs;
  unsigned int gs;
  unsigned int ldt;
  unsigned int trapbit;
  unsigned int uIOMapeoBase;
  char espacio0[1024];
  char espacio1[16];
  char espacio2[16];
  stuFpu fpu;
}stuTSS;


typedef struct {
    void * pvPuntoCarga;
    unsigned int uiTamanioTexto;
    unsigned int uiTamanioDatosInicializados;
    unsigned int uiTamanioStack;
    char stNombre [13];
} stuInfoEjecutable;







stuPCB pstuPCB[50];
stuTSS stuTSSTablaTareas[50];





typedef struct {
   char stBinario[30];
   long int liTamanio;
} stuListaEspera;


int iCantEnEspera;
stuListaEspera stListaEspera[100];
void vFnGdtInicializar(dword);

int iFnBuscaPosicionProc(unsigned long);
unsigned int uiFnSetearBaseLimiteDescriptor(int,unsigned int,unsigned int);

unsigned int iFnAgregarDescriptorGDT(unsigned int,unsigned int,unsigned int,int);

int iFnBuscarPCBLibre();
unsigned int uiFnBuscarEntradaGDTLibre();

void vFnRecorrerListaEspera();
int iFnCrearTSSReloj (void*,int,unsigned int,unsigned int,unsigned int);
int iFnCrearTSS (void*,void*,int,unsigned int,unsigned int,unsigned int,unsigned int,unsigned int,int);

int iFnCrearPCB(int,void*,char*,unsigned int,unsigned int,unsigned int,unsigned int,unsigned int,unsigned int,unsigned int,unsigned int, unsigned int, unsigned int,unsigned int);

int iFnInstanciarReloj(void *, char*);
int iFnInstanciarIdle();
int iFnInstanciarInit();

void vFnAgregarListaEspera(char*, long int);

int iFnLeerCabeceraEjecutable(char* stArchivo, stuInfoEjecutable* pstuInfo);

int iFnCrearProceso(char *,int,int);
int iFnCrearCallGate(int, unsigned short, unsigned short, unsigned char, unsigned int, unsigned int, unsigned short);
int iFnRedimensionarProceso(unsigned long ulPid, unsigned long ulBrk);

int iFnDuplicarProceso(unsigned int uiProcPadre);
int iFnEliminarProceso(unsigned int uiProcesp);
int iFnReemplazarProcesoFija( unsigned int uiProceso, unsigned char *__codigo, unsigned int size );
int iFnReemplazarProceso(unsigned long, char *, unsigned int);






char* pstFnObtenerEnviroment();


int iFnClonarProceso();
# 11 "system.c" 2
# 1 "../include/shell/shell.h" 1
# 13 "../include/shell/shell.h"
void vFnManejadorTecladoShell(unsigned char scanCode);

char cFnGetch();
char cFnPausa();
int iFnGetArg(int iPos, int iNumero, char* stResultado, int iAnchoMaximo);
int iFnXtoI (char *c);
int iFnHtoI (char *c);

void vFnAPM_info();
void vFnAPM_shutdown();

void vFnLoopReloj();
void vFnImprimirMenu ();
void vFnImprimirContexto();
void vFnMenuInstanciarInit();
void vFnMostrarListaParticiones();
void vFnBorrarUltimaPartFija();
void vFnAgregarPartFija(char*);
void vFnResetearListaPart();
int iFnShellActivo();


void vFnImprimirMenu ();
void vFnImprimirSeparadores ();




void vFnShell ();
void vFnMenuAyuda ();



void vFnMenuDump (int,int);
void vFnMenuWriteArch(int,int);
void vFnMenuOptKill(int);
void iFnTomaPrimerPara();
void iFnTomaNParametro(int,char *);
void vFnConvierteACadena(int, char *);



void vFnMenuKill (int);
void vFnMenuKillSeg (int);
void vFnMenuMem ();
void vFnMenuVer ();
void vFnMenuDesc (int,int);
void vFnMenuEjecutar(int);
void vFnMenuStack (int,int);
void vFnMenuGdt ();
void vFnMenuIdt ();
void vFnMenuCambiaTeclado (int);
void vFnMenuCls ();
void vFnMenuTSS (int);
void vFnMenuBitmap ();
void vFnMenuSegs ();
void vFnMenuPag (int);
void vFnMenuPs ();
void vFnMenuExec (int,int);
void vFnMenuExecSeg (int,int);
void vFnMenuLeerArch(int);
void vFnMenuCheck (int);
void vFnMenuSet (int);
void vFnSumaFpu (int);
void vFnMenuSumaFpu(int,int);
void vFnMenuVerSemaforos();
void vFnMenuVerShm();
void vFnMenuTestptrace(int);
void vFnMenuFd ();
void vFnMenuLs (int);
void vFnMenuCD(int);
void vFnMenuRm(int);

void vFnTermLog (unsigned char ucCaracter);

char *cpFnGetEnv(char *cpVariable);
void vFnGetAllEnv();
void vFnSetEnv(char *cpVariable, char *cpValor);
void vFnUnSetEnv(char *cpVariable);



typedef struct {
 char cpVariable[32 +1];
 char cpValor[32 +1];
 int iActiva;
}stuENV;




void vFnInicializarEnv();

stuENV pstuENV[10];


unsigned long ulPidProcesoForeground;
unsigned long uiAPM_ok;
unsigned long uiAPM_check_ax;
unsigned long uiAPM_check_bx;
unsigned long uiAPM_check_cx;

unsigned long uiAPM_32_cs;
unsigned long uiAPM_offset_entry_point;
unsigned long uiAPM_16_cs;
unsigned long uiAPM_ds;
# 12 "system.c" 2
# 1 "../include/kernel/registros.h" 1
# 15 "../include/kernel/registros.h"
word wFnGetSS();




word wFnGetES();




word wFnGetCS();




word wFnGetDS();




word wFnGetFS();




word wFnGetGS();




dword wFnGetESP();

void vFnLTR(dword);

dword uivFnSTR();
# 13 "system.c" 2

# 1 "../include/kernel/syscall.h" 1







# 1 "../include/kernel/signal.h" 1
# 9 "../include/kernel/syscall.h" 2
# 1 "../include/kernel/semaforo.h" 1
# 20 "../include/kernel/semaforo.h"
typedef struct
{
 int inicializado;
 int valor;
 unsigned long int head;
 unsigned long int tail;

 unsigned long int procesosEnCola;

 unsigned long int cola[5];
 unsigned long int pshared;
}semaforo;





typedef struct
{
 int sem_id;
}sem_t;





typedef struct{
 int pshared;
 unsigned int value;
} sem_init_params;


void vFnEncolarProceso(semaforo *sem, unsigned long int uliId);


void vFnInicializaCola(unsigned long int *cola);


int iFnHayProcesoEnEspera(semaforo *sem);


unsigned long int uliFnDesencolarProceso(semaforo *sem);


void vFnInicializarSemaforos();
# 77 "../include/kernel/semaforo.h"
int iFnSemInit(sem_t *sem, int pshared, unsigned int value);
# 86 "../include/kernel/semaforo.h"
int iFnSemClose(sem_t *sem);
# 106 "../include/kernel/semaforo.h"
int iFnSemWait(sem_t *sem);
# 116 "../include/kernel/semaforo.h"
int iFnSemPost(sem_t *sem);

int validaExistenciaSemaforo(sem_t *sem);


int agregarSemaforoAlSistema(int pshared, unsigned int value);

void vFnVerSemaforos();
# 10 "../include/kernel/syscall.h" 2
# 195 "../include/kernel/syscall.h"
enum __ptrace_request {
 PTRACE_TRACEME,
 PTRACE_PEEKTEXT,
 PTRACE_PEEKDATA,
 PTRACE_PEEKUSER,
 PTRACE_POKETEXT,
 PTRACE_POKEDATA,
 PTRACE_POKEUSER,
 PTRACE_GETREGS,
 PTRACE_GETFPREGS,
 PTRACE_SETREGS,
 PTRACE_SETFPREGS,
 PTRACE_CONT,
 PTRACE_KILL,
 PTRACE_ATTACH,
 PTRACE_DETACH
};


typedef struct {
 int iPid;
 void *pvAddr;
 void *pvData;
}__ptrace_param;



typedef struct _stuRegs_
{
  long ebx;
  long ecx;
  long edx;
  long esi;
  long edi;
  long ebp;
  long eax;
  int xds;
  int xes;
  long orig_eax;
  long eip;
  int xcs;
  long eflags;
  long esp;
  int xss;
} stuRegs;






long lFnSysExit( int status );
long lFnSysFork();
long lFnSysTest( long num );
long lFnSysRead( int, void *buf, size_t count );
long lFnSysWrite( int, const void *buf, size_t count );
long lFnSysWaitPid( int pid, int *status, int options );
long lFnSysExecve( const char *filename, char *argv[], char *envp[] );
long lFnSysTime( long *t );
long lFnSysGetPid();
long lFnSysKill( int pid, int sig );
long lFnSysGetPPid();
long lFnSysReboot( int flag );
long lFnSysSodiumCallgate(int iOpcion, int iPar1, int iPar2);
long lFnSysBrk(unsigned long);
long lFnSysSBrk(unsigned long);
long lFnSysSemInit( sem_t *sem, sem_init_params * params);
long lFnSysSemPost( sem_t *sem );
long lFnSysSemWait( sem_t *sem );
long lFnSysSemClose( sem_t *sem );
long lFnSysShmGet( key_t key, size_t size );
long lFnSysShmAt( int shmid, void * shmAddr );
long lFnSysShmDt( key_t key );

int iFnSysSumar(int a, int b, int * res);



long lFnSysNice( int );
long lFnSysGetpriority( int, int );
long lFnSysSetpriority( int, int, int );
long lFnSysClone(stuRegs *,int flags);


long lFnSysIdle();



long lFnSysSchedSetParam(int p);
long lFnSysSchedGetParam();
long lFnSysSchedSetScheduler(int p);
long lFnSysSchedGetScheduler();
long lFnSysSchedYield();
long lFnSysSchedGetPriorityMax();
long lFnSysSchedGetPriorityMin();
long lFnSysSchedRrGetInterval();





long lFnSysStime(time_t *newtime);
long lFnSysTimes(tms *ptmsbuffer);
long lFnSysGettimeofday(timeval *ptimervalTp, timezone *ptimezoneTzp);
long lFnSysSettimeofday(timeval *ptimevalTp, timezone *ptimezoneTzp);
long lFnSysGettimer(int iWhich, itimerval *pitimervalValue);
long lFnSysSettimer(int iWhich, itimerval const *pcnitimervalValue, itimerval *ptimervalOvalue);
long lFnSysAdjtimex(timex *ptimexBuffer);
long lFnSysNanosleep(timespec const *pcntimespecRequested_time, timespec *ptimespecRemaining);



long lFnSysPtrace( int request, void *pvDirParam );





unsigned long ulFnSysBrk(unsigned long brk);
# 15 "system.c" 2
# 1 "../include/usr/reloj.h" 1
# 12 "../include/usr/reloj.h"
void vFnLoopReloj();

void vFnInicializarReloj();
# 16 "system.c" 2
# 1 "../include/kernel/drivers/floppy.h" 1







# 1 "../include/kernel/drivers/dma.h" 1
# 89 "../include/kernel/drivers/dma.h"
void inicDMA();
void setCanalDMA(u8 Canal, u32 Direccion, u8 Modo, u16 Cuenta);
void setMascaraDMA(u8 Mascara);
void setDirDMA(u8 Canal, u32 Direccion);
void setContDMA(u8 Canal, u16 Cuenta);
void setModoDMA(u8 Modo);
void habilitarCanal(u8 Canal);
void inhibirCanal(u8 Canal);
u8 peticionDMA(u8 Canal);
u8 TCDMA(u8 Canal);
# 9 "../include/kernel/drivers/floppy.h" 2
# 19 "../include/kernel/drivers/floppy.h"
typedef struct {u8 RST0; u8 Valido;} ST0_t;
typedef struct {u8 RST1; u8 Valido;} ST1_t;
typedef struct {u8 RST2; u8 Valido;} ST2_t;




typedef struct
{
 u8 Cilindro;
 u8 Cabeza;
 u8 Sector;
}
CHS;




typedef enum
{
 Termin_Normal, Term_Anormal, Com_No_Finalizado, Com_Invalido
}Estado;


void* pvBufferDmaFloppy;
byte byBufferDmaNoAlineadoFloppy[512*2];
void* alinearBufferFloppyDMA(void* Buffer, unsigned long ulTamanioTotal);

u8 leerSector (byte* Buffer, u16 NBloque);
u8 escribirSector (byte* Buffer, u16 NBloque);
u8 operExitosa ();
void prenderMotor ();
void apagarMotor ();
u8 envComRecalibrado ();
u8 envComDesplCabeza (u8 Cilindro, u8 Cabeza);
u8 envComLectura (u8 Cilindro, u8 Cabeza, u8 Sector);
u8 envComEscritura (u8 Cilindro, u8 Cabeza, u8 Sector);
void enviarDatosOper (u8 Cilindro, u8 Cabeza, u8 Sector);
u8 leerEstadoFDC ();
void leerST0 ();
u8 leerST3 ();
u8 vFnImprimirST0 ();
void vFnImprimirST1 ();
void vFnImprimirST2 ();
void vFnImprimirST3 ();
void vFnImprimirEstadoFDC ();
u8 listoPES ();
u8 esperandoByteProc ();
u8 bytePProcesador ();
u8 FDCOcupado ();
u8 unidadOcupada ();
void obtResOper ();
void invalidarEstados ();
CHS LBAaCHS (u16 NBloque);

void ISRDisketeraC();
# 17 "system.c" 2



short int siEjecutoSyscall = 0;
extern dword pdwGDT;

extern stuPCB pstuPCB[50];
extern stuTSS stuTSSTablaTareas[50];

extern unsigned long ulProcActual;
extern unsigned int uiUltimoPid;
extern unsigned long ulUltimoProcesoEnFPU;
short int bPlanificador = 1;



unsigned long ulTiempo = 0;


int iMinuteswest = 0;



unsigned int iMilisegundosPorTick = (1000 * (0x10000-1)) / 1193181l;


unsigned int uiTicsCorrigeDesvio = (0x10000-1);

long lRelojOffset = 0;
long lRelojFrequencia = 0;
long lRelojMaxerror = 432;
long lRelojEsterror = 0;
long lRelojConstante = 0;

int iRelojEstado = 0;

void vFnAsignarFuncionExcepcion (int iNumeroExcepcion,
     void (*pvFnFuncion) ());







void
vFnMostrarRegistrosvCPU ()
{

  stuGDTDescriptor *pstuGDTDescriptorDescriptor;

  unsigned int eax;
  unsigned int ecx;
  unsigned int edx;
  unsigned int ebx;
  unsigned int esp;
  unsigned int ebp;
  unsigned int esi;
  unsigned int edi;
  vFnImprimir ("\ntr=%x, ss= %x, esp= %x, cs= %x, ds= %x, fs= %x",
        uiFnSTR (), wFnGetSS (), wFnGetESP (), wFnGetCS (),
        wFnGetDS (), wFnGetFS (), wFnGetGS ());
asm ("mov %%eax, %0\n\t" "mov %%ecx, %1\n\t" "mov %%edx, %2\n\t" "mov %%ebx, %3\n\t" "mov %%esp, %4\n\t" "mov %%ebp, %5\n\t" "mov %%esi, %6\n\t" "mov %%edi, %7\n\t":"=m" (eax), "=m" (ecx), "=m" (edx), "=m" (ebx), "=m" (esp), "=m" (ebp),
       "=m" (esi),
       "=m" (edi));

  vFnImprimir
    ("\neax= 0x%xh, ecx= 0x%xh, edx= 0x%xh, ebx= 0x%xh\nesp= 0x%xh, ebp= 0x%xh, esi= 0x%xh, edi= 0x%xh",
     eax, ecx, edx, ebx, esp, ebp, esi, edi);

  pstuGDTDescriptorDescriptor =
    &(pstuTablaGdt->stuGdtDescriptorDescs[(uiFnSTR () / 8)]);

  vFnImprimir ("\n\nDescriptor GDT: ");
  vFnImprimir ("\nlimit_low=%x", pstuGDTDescriptorDescriptor->usLimiteBajo);

  vFnImprimir ("\nbase_low=%x", pstuGDTDescriptorDescriptor->usBaseBajo);
  vFnImprimir ("\nbase_med=%x", pstuGDTDescriptorDescriptor->ucBaseMedio);
  vFnImprimir ("access=%x", pstuGDTDescriptorDescriptor->ucAcesso);
  vFnImprimir ("\nlimit_high=%x", pstuGDTDescriptorDescriptor->bitLimiteAlto);
  vFnImprimir ("\ngranularity=%x",
        pstuGDTDescriptorDescriptor->bitGranularidad);
  vFnImprimir ("\nbase_high=%x", pstuGDTDescriptorDescriptor->usBaseAlto);

}
# 140 "system.c"
void
vFnExcepcionCPU0 ()
{
  long eip;
  asm volatile ("cli"::);
  __asm__ volatile ( "movl	%%ss, %%eax		\n\t" "movl	%%esp, %%ebx		\n\t" "pushw	$0x10 			\n\t" "popw	%%ss 			\n\t" "movl	$0x200000, %%esp	\n\t" "pushl	%%ebx 			\n\t" "pushl	%%eax 			\n\t" "pushw	$0x10	 		\n\t" "popw	%%ds 			\n\t" "pushw	$0x10 			\n\t" "popw	%%es 			\n\t" "pushw	$0x10 			\n\t" "popw	%%fs 			\n\t" "pushw	$0x10 			\n\t" "popw	%%gs 			\n\t" :: );
  __asm__ volatile( "movl  (%%esp), %%eax  \n\t" "movl 4(%%esp), %%ebx  \n\t" "movl	%%eax, %%ds	 \n\t" "movl	4(%%ebx), %%eax	 \n\t" "pushw $0x10		 \n\t" "popw	%%ds 		 \n\t" "movl	%%eax, %0	 \n\t" : "=m"( eip ) );
  vFnImprimir ("\n \n Error de Division por Cero... (EIP: 0x%x)", eip );
  asm volatile ("hlt"::);
}






void
vFnExcepcionCPU1 ()
{
  asm volatile ("cli"::);
  __asm__ volatile ( "movl	%%ss, %%eax		\n\t" "movl	%%esp, %%ebx		\n\t" "pushw	$0x10 			\n\t" "popw	%%ss 			\n\t" "movl	$0x200000, %%esp	\n\t" "pushl	%%ebx 			\n\t" "pushl	%%eax 			\n\t" "pushw	$0x10	 		\n\t" "popw	%%ds 			\n\t" "pushw	$0x10 			\n\t" "popw	%%es 			\n\t" "pushw	$0x10 			\n\t" "popw	%%fs 			\n\t" "pushw	$0x10 			\n\t" "popw	%%gs 			\n\t" :: );
  vFnImprimir ("\nDebug Exception...");
  asm volatile ("hlt"::);
}






void
vFnExcepcionCPU2 ()
{
  asm volatile ("cli"::);
  __asm__ volatile ( "movl	%%ss, %%eax		\n\t" "movl	%%esp, %%ebx		\n\t" "pushw	$0x10 			\n\t" "popw	%%ss 			\n\t" "movl	$0x200000, %%esp	\n\t" "pushl	%%ebx 			\n\t" "pushl	%%eax 			\n\t" "pushw	$0x10	 		\n\t" "popw	%%ds 			\n\t" "pushw	$0x10 			\n\t" "popw	%%es 			\n\t" "pushw	$0x10 			\n\t" "popw	%%fs 			\n\t" "pushw	$0x10 			\n\t" "popw	%%gs 			\n\t" :: );
  vFnImprimir ("\nExcepcion Desconocida...");
  asm volatile ("hlt"::);
}






void
vFnExcepcionCPU3 ()
{
  asm volatile ("cli"::);
  __asm__ volatile ( "movl	%%ss, %%eax		\n\t" "movl	%%esp, %%ebx		\n\t" "pushw	$0x10 			\n\t" "popw	%%ss 			\n\t" "movl	$0x200000, %%esp	\n\t" "pushl	%%ebx 			\n\t" "pushl	%%eax 			\n\t" "pushw	$0x10	 		\n\t" "popw	%%ds 			\n\t" "pushw	$0x10 			\n\t" "popw	%%es 			\n\t" "pushw	$0x10 			\n\t" "popw	%%fs 			\n\t" "pushw	$0x10 			\n\t" "popw	%%gs 			\n\t" :: );
  vFnImprimir ("\nBreakpoint No Esperado...");
  asm volatile ("hlt"::);
}






void
vFnExcepcionCPU4 ()
{
  asm volatile ("cli"::);
  __asm__ volatile ( "movl	%%ss, %%eax		\n\t" "movl	%%esp, %%ebx		\n\t" "pushw	$0x10 			\n\t" "popw	%%ss 			\n\t" "movl	$0x200000, %%esp	\n\t" "pushl	%%ebx 			\n\t" "pushl	%%eax 			\n\t" "pushw	$0x10	 		\n\t" "popw	%%ds 			\n\t" "pushw	$0x10 			\n\t" "popw	%%es 			\n\t" "pushw	$0x10 			\n\t" "popw	%%fs 			\n\t" "pushw	$0x10 			\n\t" "popw	%%gs 			\n\t" :: );
  vFnImprimir ("\nError de Desbordamiento...");
  asm volatile ("hlt"::);
}






void
vFnExcepcionCPU5 ()
{
  asm volatile ("cli"::);
  __asm__ volatile ( "movl	%%ss, %%eax		\n\t" "movl	%%esp, %%ebx		\n\t" "pushw	$0x10 			\n\t" "popw	%%ss 			\n\t" "movl	$0x200000, %%esp	\n\t" "pushl	%%ebx 			\n\t" "pushl	%%eax 			\n\t" "pushw	$0x10	 		\n\t" "popw	%%ds 			\n\t" "pushw	$0x10 			\n\t" "popw	%%es 			\n\t" "pushw	$0x10 			\n\t" "popw	%%fs 			\n\t" "pushw	$0x10 			\n\t" "popw	%%gs 			\n\t" :: );
  vFnImprimir ("\nError en Chequeo de Limites...");
  asm volatile ("hlt"::);
}






void
vFnExcepcionCPU6 ()
{
  asm volatile ("cli"::);
  __asm__ volatile ( "movl	%%ss, %%eax		\n\t" "movl	%%esp, %%ebx		\n\t" "pushw	$0x10 			\n\t" "popw	%%ss 			\n\t" "movl	$0x200000, %%esp	\n\t" "pushl	%%ebx 			\n\t" "pushl	%%eax 			\n\t" "pushw	$0x10	 		\n\t" "popw	%%ds 			\n\t" "pushw	$0x10 			\n\t" "popw	%%es 			\n\t" "pushw	$0x10 			\n\t" "popw	%%fs 			\n\t" "pushw	$0x10 			\n\t" "popw	%%gs 			\n\t" :: );
  vFnImprimir ("\nOpCode Invalido...");
  asm volatile ("hlt"::);
}






void vFnExcepcionCPU7 () {


  stuTSS * stuTSSProcActual, * stuTSSProcAnterior;


  asm volatile ("clts \n");



  if ( ulUltimoProcesoEnFPU != pstuPCB[ulProcActual].ulId )
  {
   stuTSSProcActual = &stuTSSTablaTareas[ pstuPCB[ulProcActual].ulLugarTSS ];
   stuTSSProcAnterior = &stuTSSTablaTareas[pstuPCB[iFnBuscaPosicionProc(ulUltimoProcesoEnFPU)].ulLugarTSS ];

   if( ulUltimoProcesoEnFPU )
   {



    asm volatile(
      "fnsave %0\n"
      "fwait	\n"
      : "=m" (stuTSSProcAnterior->fpu) );
   }




   asm volatile(
     "frstor %0\n"
     : : "m" (stuTSSProcActual->fpu) );

   ulUltimoProcesoEnFPU = pstuPCB[ulProcActual].ulId;
  }





}






void
vFnExcepcionCPU8 ()
{
  asm volatile ("cli"::);
  __asm__ volatile ( "movl	%%ss, %%eax		\n\t" "movl	%%esp, %%ebx		\n\t" "pushw	$0x10 			\n\t" "popw	%%ss 			\n\t" "movl	$0x200000, %%esp	\n\t" "pushl	%%ebx 			\n\t" "pushl	%%eax 			\n\t" "pushw	$0x10	 		\n\t" "popw	%%ds 			\n\t" "pushw	$0x10 			\n\t" "popw	%%es 			\n\t" "pushw	$0x10 			\n\t" "popw	%%fs 			\n\t" "pushw	$0x10 			\n\t" "popw	%%gs 			\n\t" :: );
  vFnImprimir ("\nDouble Fault...");
  asm volatile ("hlt"::);
}






void* pvFnKernelStackAdmin()
 {

    unsigned int uiTamanioStackInterrupcion;
 uiTamanioStackInterrupcion = 2000;
 if(uiHeapKernelInicializado == 0) return 60000;
 if(uiHeapKernelInicializado == 1) return (pvFnReservarParticionUsuario(uiTamanioStackInterrupcion) + uiTamanioStackInterrupcion - 100);
 }







void
vFnExcepcionCPU9 ()
{
  asm volatile ("cli"::);
  __asm__ volatile ( "movl	%%ss, %%eax		\n\t" "movl	%%esp, %%ebx		\n\t" "pushw	$0x10 			\n\t" "popw	%%ss 			\n\t" "movl	$0x200000, %%esp	\n\t" "pushl	%%ebx 			\n\t" "pushl	%%eax 			\n\t" "pushw	$0x10	 		\n\t" "popw	%%ds 			\n\t" "pushw	$0x10 			\n\t" "popw	%%es 			\n\t" "pushw	$0x10 			\n\t" "popw	%%fs 			\n\t" "pushw	$0x10 			\n\t" "popw	%%gs 			\n\t" :: );
  vFnImprimir ("\nCoprocessor Segment Overrun...");
  asm volatile ("hlt"::);
}






void
vFnExcepcionCPU10 ()
{
  asm volatile ("cli"::);
  __asm__ volatile ( "movl	%%ss, %%eax		\n\t" "movl	%%esp, %%ebx		\n\t" "pushw	$0x10 			\n\t" "popw	%%ss 			\n\t" "movl	$0x200000, %%esp	\n\t" "pushl	%%ebx 			\n\t" "pushl	%%eax 			\n\t" "pushw	$0x10	 		\n\t" "popw	%%ds 			\n\t" "pushw	$0x10 			\n\t" "popw	%%es 			\n\t" "pushw	$0x10 			\n\t" "popw	%%fs 			\n\t" "pushw	$0x10 			\n\t" "popw	%%gs 			\n\t" :: );
  vFnImprimir ("\nTSS Invalida...");
  asm volatile ("hlt"::);
}






void
vFnExcepcionCPU11 ()
{
  asm volatile ("cli"::);
  __asm__ volatile ( "movl	%%ss, %%eax		\n\t" "movl	%%esp, %%ebx		\n\t" "pushw	$0x10 			\n\t" "popw	%%ss 			\n\t" "movl	$0x200000, %%esp	\n\t" "pushl	%%ebx 			\n\t" "pushl	%%eax 			\n\t" "pushw	$0x10	 		\n\t" "popw	%%ds 			\n\t" "pushw	$0x10 			\n\t" "popw	%%es 			\n\t" "pushw	$0x10 			\n\t" "popw	%%fs 			\n\t" "pushw	$0x10 			\n\t" "popw	%%gs 			\n\t" :: );
  vFnImprimir ("\nSegmento No Presente...");
  asm volatile ("hlt"::);
}






void
vFnExcepcionCPU12 ()
{
  asm volatile ("cli"::);
  __asm__ volatile ( "movl	%%ss, %%eax		\n\t" "movl	%%esp, %%ebx		\n\t" "pushw	$0x10 			\n\t" "popw	%%ss 			\n\t" "movl	$0x200000, %%esp	\n\t" "pushl	%%ebx 			\n\t" "pushl	%%eax 			\n\t" "pushw	$0x10	 		\n\t" "popw	%%ds 			\n\t" "pushw	$0x10 			\n\t" "popw	%%es 			\n\t" "pushw	$0x10 			\n\t" "popw	%%fs 			\n\t" "pushw	$0x10 			\n\t" "popw	%%gs 			\n\t" :: );
  vFnImprimir ("\nExcepcion de Stack...");
  asm volatile ("hlt"::);
}






void vFnExcepcionCPU13 () {

    vFnImprimir("\nExcepcion de Proteccion General (ExcepcionCPU13):");
    vFnMostrarRegistrosvCPU ();

    vFnLog("\nExcepcion de Proteccion General (ExcepcionCPU13):");
    vFnLog(" Proceso actual PID=%d \"%s\" ", pstuPCB[ulProcActual].ulId,
            pstuPCB[ulProcActual].stNombre);
# 389 "system.c"
    lFnSysKill( pstuPCB[ulProcActual].ulId, 11 );
    vFnPlanificador();
}
# 400 "system.c"
void
vFnExcepcionCPU14 ()
{
  asm volatile ("cli"::);
  __asm__ volatile ( "movl	%%ss, %%eax		\n\t" "movl	%%esp, %%ebx		\n\t" "pushw	$0x10 			\n\t" "popw	%%ss 			\n\t" "movl	$0x200000, %%esp	\n\t" "pushl	%%ebx 			\n\t" "pushl	%%eax 			\n\t" "pushw	$0x10	 		\n\t" "popw	%%ds 			\n\t" "pushw	$0x10 			\n\t" "popw	%%es 			\n\t" "pushw	$0x10 			\n\t" "popw	%%fs 			\n\t" "pushw	$0x10 			\n\t" "popw	%%gs 			\n\t" :: );
  vFnImprimir ("\nPage Fault...");
  asm volatile ("hlt"::);
}






void
vFnExcepcionCPU15 ()
{
  asm volatile ("cli"::);
  __asm__ volatile ( "movl	%%ss, %%eax		\n\t" "movl	%%esp, %%ebx		\n\t" "pushw	$0x10 			\n\t" "popw	%%ss 			\n\t" "movl	$0x200000, %%esp	\n\t" "pushl	%%ebx 			\n\t" "pushl	%%eax 			\n\t" "pushw	$0x10	 		\n\t" "popw	%%ds 			\n\t" "pushw	$0x10 			\n\t" "popw	%%es 			\n\t" "pushw	$0x10 			\n\t" "popw	%%fs 			\n\t" "pushw	$0x10 			\n\t" "popw	%%gs 			\n\t" :: );
  vFnImprimir ("\nExcepcion Desconocida...");
  asm volatile ("hlt"::);
}






void vFnExcepcionCPU16 () {

  u16 usiEstadoFPU;
  u16 usiControlFPU;
  u32 aux;


  asm volatile( "fnstsw %0\n"
      "fnstcw %1\n"
     : "=m" (usiEstadoFPU),
       "=m" (usiControlFPU));


  asm volatile( "fnclex" );


  usiEstadoFPU = (~usiControlFPU) & usiEstadoFPU;
  usiEstadoFPU &= 0x003F;



  switch (usiEstadoFPU) {
 case 0x0001:
    vFnImprimir("\nFPU: Error de Operacion Invalida");
    break;
 case 0x0002:
    vFnImprimir("\nFPU: Error de Operando Denormalizado");
    break;
   case 0x0004:
    vFnImprimir("\nFPU: Error de Division por Cero");
    break;
 case 0x0008:
    vFnImprimir("\nFPU: Error de Overflow");
    break;
   case 0x0010:
      vFnImprimir("\nFPU: Error de Underflow");
      break;
   case 0x0020:
    vFnImprimir("\nFPU: Error de Precision");
    break;
 default:
    vFnImprimir("\nFPU: Error no especificado");
    break;
  }


  lFnSysKill(pstuPCB[ulProcActual].ulId,8);





}
# 490 "system.c"
void
vFnIniciarExcepciones ()
{
  int iN = 17;

  vFnAsignarFuncionExcepcion (0, vFnExcepcionCPU0);
  vFnAsignarFuncionExcepcion (1, vFnExcepcionCPU1);
  vFnAsignarFuncionExcepcion (2, vFnExcepcionCPU2);
  vFnAsignarFuncionExcepcion (3, vFnExcepcionCPU3);
  vFnAsignarFuncionExcepcion (4, vFnExcepcionCPU4);
  vFnAsignarFuncionExcepcion (5, vFnExcepcionCPU5);
  vFnAsignarFuncionExcepcion (6, vFnExcepcionCPU6);
  vFnAsignarFuncionExcepcion (7, vFnExcepcionCPU7_Asm);
  vFnAsignarFuncionExcepcion (8, vFnExcepcionCPU8);
  vFnAsignarFuncionExcepcion (9, vFnExcepcionCPU9);
  vFnAsignarFuncionExcepcion (10, vFnExcepcionCPU10);
  vFnAsignarFuncionExcepcion (11, vFnExcepcionCPU11);
  vFnAsignarFuncionExcepcion (12, vFnExcepcionCPU12);
  vFnAsignarFuncionExcepcion (13, vFnExcepcionCPU13_Asm);
  vFnAsignarFuncionExcepcion (14, vFnExcepcionCPU14);
  vFnAsignarFuncionExcepcion (15, vFnExcepcionCPU15);
  vFnAsignarFuncionExcepcion (16, vFnExcepcionCPU16_Asm);

  for (; iN < 31; iN++)
    {
      vFnAsignarFuncionExcepcion (iN, vFnExcepcionCPU15);
    }
}






void
vFnAsignarFuncionExcepcion (int iNumeroExcepcion, void (*pvFnFuncion) ())
{
  struct stuDireccion
  {
    unsigned short uwDireccionBaja;
    unsigned short uwDireccionAlta;
  };
  union
  {
    void (*pvFnFuncion) ();
    struct stuDireccion stuDireccionFuncion;
  } unDireccionFuncion;

  unDireccionFuncion.pvFnFuncion = pvFnFuncion;

  pstuIDT->IDTDescriptor[iNumeroExcepcion].InterruptGate.Offset1 =
    unDireccionFuncion.stuDireccionFuncion.uwDireccionBaja;
  pstuIDT->IDTDescriptor[iNumeroExcepcion].InterruptGate.SegmentSelector =
    0x8;
  pstuIDT->IDTDescriptor[iNumeroExcepcion].InterruptGate.Type = 0x70;
  pstuIDT->IDTDescriptor[iNumeroExcepcion].InterruptGate.DPL = 0;
  pstuIDT->IDTDescriptor[iNumeroExcepcion].InterruptGate.P = 1;
  pstuIDT->IDTDescriptor[iNumeroExcepcion].InterruptGate.Offset2 =
    unDireccionFuncion.stuDireccionFuncion.uwDireccionAlta;

}
# 562 "system.c"
void
vFnIniciarIDT ()
{

  int iN;
  void *pvFnFuncion;
  dword dwAux, dwAux2;

  pvFnFuncion = (void *) vFnHandlerGenerico_Asm;


  dwAux = (word) ((dword) pvFnFuncion & 0x0000FFFF);
  dwAux2 = (word) ((dword) pvFnFuncion >> 16);


  for (iN = 0; iN < sizeof(stuIDT) / sizeof(stuIDTDescriptor); iN++)
    {
      pstuIDT->IDTDescriptor[iN].InterruptGate.Offset1 = dwAux;
      pstuIDT->IDTDescriptor[iN].InterruptGate.SegmentSelector = 0x8;
      pstuIDT->IDTDescriptor[iN].InterruptGate.Type = 0x70;
      pstuIDT->IDTDescriptor[iN].InterruptGate.DPL = 0;
      pstuIDT->IDTDescriptor[iN].InterruptGate.P = 1;
      pstuIDT->IDTDescriptor[iN].InterruptGate.Offset2 = dwAux2;
    }



  pvFnFuncion = (void *) vFnHandlerTimer_Asm;
  dwAux = (word) ((dword) pvFnFuncion & 0x0000FFFF);
  dwAux2 = (word) ((dword) pvFnFuncion >> 16);
  pstuIDT->IDTDescriptor[32].InterruptGate.Offset1 = dwAux;
  pstuIDT->IDTDescriptor[32].InterruptGate.Offset2 = dwAux2;



  pvFnFuncion = (void *) vFnHandlerTeclado_Asm;
  dwAux = (word) ((dword) pvFnFuncion & 0x0000FFFF);
  dwAux2 = (word) ((dword) pvFnFuncion >> 16);
  pstuIDT->IDTDescriptor[33].InterruptGate.Offset1 = dwAux;
  pstuIDT->IDTDescriptor[33].InterruptGate.Offset2 = dwAux2;



  pvFnFuncion = (void *) vFnHandlerSyscall_Asm;
  dwAux = (word) ((dword) pvFnFuncion & 0x0000FFFF);
  dwAux2 = (word) ((dword) pvFnFuncion >> 16);
  pstuIDT->IDTDescriptor[0x80].InterruptGate.DPL = 3;
  pstuIDT->IDTDescriptor[0x80].InterruptGate.Offset1 = dwAux;
  pstuIDT->IDTDescriptor[0x80].InterruptGate.Offset2 = dwAux2;




  pvFnFuncion = (void *) vFnHandlerDisquette_Asm;
  dwAux = (word) ((dword) pvFnFuncion & 0x0000FFFF);
  dwAux2 = (word) ((dword) pvFnFuncion >> 16);
  pstuIDT->IDTDescriptor[38].InterruptGate.Offset1 = dwAux;
  pstuIDT->IDTDescriptor[38].InterruptGate.Offset2 = dwAux2;


  vFnRemapearPIC_Asm ();


  vFnIniciarTeclado();


  vFnIniciarTimer_Asm ((0x10000-1));


  vFnIniciarIDT_Asm (sizeof(stuIDT), pstuIDT);

}
# 644 "system.c"
void
vFnHandlerGenerico ()
{
  vFnLog("\nEl manejo de esta interrupcion todavia no esta desarrollado\n");
}
# 657 "system.c"
long
lFnHandlerSyscall ( long eax, long ebx, long ecx, long edx, long edi )
{







 if (pstuPCB[ulProcActual].lPidTracer == 1)
 {


  lFnSysKill(pstuPCB[ulProcActual].ulParentId,18);
  lFnSysKill(pstuPCB[ulProcActual].ulId,19);
 }

 switch( eax ){
  case( 1 ): return lFnSysExit( (int)ebx );
  case( 2 ): return lFnSysFork();
  case( 3 ): return lFnSysTest( ebx );
  case( 4 ): return lFnSysRead( (int)ebx,
              (void*)ecx,
        (size_t)edx );
  case( 5 ): return lFnSysWrite( (int)ebx,
               (const void*)ecx,
         (size_t)edx );
  case( 7 ): return lFnSysWaitPid( (int) ebx,
                 (int*) ecx,
           (int) edx );
  case( 11 ): return lFnSysExecve( (const char*) ebx,
                 (char** const) ecx,
           (char** const) edx );
  case( 13 ): return lFnSysTime( (long*) ebx );
  case( 20 ): return lFnSysGetPid();
  case( 37 ): return lFnSysKill( (int) ebx, (int) ecx );
  case( 64 ): return lFnSysGetPPid();
  case( 88 ): return lFnSysReboot( (int) ebx );
  case( 45 ): return lFnSysBrk( (unsigned long) ebx );
  case( 46 ): return lFnSysSBrk( (unsigned long) ebx );

  case( 200 ): return lFnSysSemInit( (sem_t *) ebx, (sem_init_params *) ecx);
  case( 202 ): return lFnSysSemPost( (sem_t *) ebx );
  case( 203 ): return lFnSysSemWait( (sem_t *) ebx );
  case( 201 ): return lFnSysSemClose( (sem_t *) ebx );
  case( 204 ): return lFnSysShmGet( (key_t) ebx, (size_t) ecx );
  case( 205 ): return lFnSysShmAt( (int) ebx, (void *) ecx );
  case( 206 ): return lFnSysShmDt( (int) ebx );
  case( 500 ): return iFnSysSumar( (int) ebx,(int) ecx,(int *) edx );

  case( 97 ): return lFnSysSetpriority( (int) ebx,(int) ecx,(int ) edx );
  case( 96 ): return lFnSysGetpriority( (int) ebx,(int) ecx);
  case( 34 ): return lFnSysNice( (int) ebx );
  case( 120 ): return lFnSysClone((stuRegs *) ebx, (int) edx);

  case( 25 ): return lFnSysStime( (time_t *) ebx );
  case( 43 ): return lFnSysTimes( (tms *) ebx );
  case( 78 ): return lFnSysGettimeofday( (timeval *) ebx, (timezone *) ecx );
  case( 79 ): return lFnSysSettimeofday( (timeval *) ebx, (timezone *) ecx );
  case( 104 ): return lFnSysGettimer( (int) ebx, (itimerval *) ecx);
  case( 105 ): return lFnSysSettimer( (int) ebx, (itimerval *) ecx, (itimerval*) edx );
  case( 124 ): return lFnSysAdjtimex( (timex *) ebx );
  case( 162 ): return lFnSysNanosleep( (timespec *) ebx, (timespec *) ecx );


  case( 112): return lFnSysIdle();
  case( 250): return lFnSysSchedSetParam( (int)ebx );
  case( 251): return lFnSysSchedGetParam();
  case( 252): return lFnSysSchedSetScheduler( (int) ebx);
  case( 253): return lFnSysSchedGetScheduler();
  case( 254): return lFnSysSchedYield();
  case( 255): return lFnSysSchedGetPriorityMax ();
  case( 256): return lFnSysSchedGetPriorityMin ();
  case( 257): return lFnSysSchedRrGetInterval ();


  case( 26 ): return lFnSysPtrace( (int) ebx, (void *) ecx );


  }

 return -2;
}
# 749 "system.c"
long
lFnHandlerCallgate ( long eax, long ebx, long ecx, long edx, long edi )
{







 if (pstuPCB[ulProcActual].lPidTracer == 1)
 {


  lFnSysKill(pstuPCB[ulProcActual].ulParentId,18);
  lFnSysKill(pstuPCB[ulProcActual].ulId,19);
 }

 switch( eax ){
  case( 1 ): return lFnSysSodiumCallgate( (int) ebx,(int) ecx,(int) edx );
  case( 2 ): return lFnSysPorcentaje( (int) ebx );
  }

 return -2;
}
# 784 "system.c"
long lFnConvertirAUnsignedLong(timeval timevalTiempo){
 long lMilisegs = timevalTiempo.tv_sec * 1000;
 lMilisegs+= timevalTiempo.tv_usec;
 return lMilisegs;
}
# 799 "system.c"
timeval timevalFnConvertirATimeval(long lMilisegs){
 timeval timevalTiempo;
 timevalTiempo.tv_sec = lMilisegs / 1000;
 timevalTiempo.tv_usec = lMilisegs - (timevalTiempo.tv_sec * 1000);
 return timevalTiempo;
}
# 818 "system.c"
void vFnDecrementarTimer(stuPCB *pstuProc, int iTimer, int iSenial, int iMilisPorTick){
    long lTimer = lFnConvertirAUnsignedLong(pstuProc->timers[iTimer].it_value);

    if (pstuProc->iEstado>-1 && pstuProc->iEstado<4 && lTimer > 0){
      lTimer-= iMilisPorTick;
      if (lTimer <= 0){

        lTimer = lFnConvertirAUnsignedLong(pstuProc->timers[iTimer].it_interval);
 lFnSysKill(pstuProc->ulId, iSenial);
      }
      pstuProc->timers[iTimer].it_value = timevalFnConvertirATimeval(lTimer);
    }

}
# 841 "system.c"
void
vFnHandlerTimer ()
{
  int i;
  stuPCB *pstuProc;
  uliClockTick++;


  ulTiempo+=iMilisegundosPorTick;
  uiTicsCorrigeDesvio--;
  if (uiTicsCorrigeDesvio == 0)
  {
  vFnInicializarReloj();
 uiTicsCorrigeDesvio = (0x10000-1);
  }
  for(i=0; i<50; i++){
    pstuProc = &pstuPCB[i];


    if (pstuProc->lNanosleep > 0 && pstuProc->iEstado == 3 && pstuPCB->ulId < uiUltimoPid){
      pstuProc->lNanosleep-=iMilisegundosPorTick;
      if (pstuProc->lNanosleep <= 0){
        pstuProc->iEstado = 1;
       pstuProc->puRestoDelNanosleep = 0;
      }
    }


    vFnDecrementarTimer(pstuProc, 0, 14, iMilisegundosPorTick);

    if (ulProcActual == i){

      vFnDecrementarTimer(pstuProc, 2, 27, iMilisegundosPorTick);


      if (siEjecutoSyscall == 1){

        pstuProc->stuTmsTiemposProceso.tms_utime++;
        pstuProc->stuTmsTiemposProceso.tms_stime++;
        vFnDecrementarTimer(pstuProc, 1, 26, iMilisegundosPorTick / 2);
   siEjecutoSyscall = 0;
      } else {
        vFnDecrementarTimer(pstuProc, 1, 26, iMilisegundosPorTick);
        pstuProc->stuTmsTiemposProceso.tms_utime+=2;
      }
    }
  }


  uliTimeSlice++;




 if(bPlanificador==1){
 if(uliTimeSlice>=uliQuantum){
   uliTimeSlice=0;
   vFnPlanificador();
   }
 }
 if(bPlanificador==2){
 if(uliTimeSlice>=uliBTSQ){
   uliTimeSlice=0;
   vFnPlanificador();
   }
 }


}






void
vFnPlanificador()
{


  vFnCopiarVariablesCompartidas();

  if(bPlanificador==1){
   vFnPlanificadorRR();

   return;
  }
  if(bPlanificador==0){
   uliTimeSlice=0;
   vFnPlanificadorRR();
   return;




  }
  if(bPlanificador==2){
   vFnPlanificadorBTS();
   return;
  }

}






void vFnIniciarTeclado()
{

ucFnObtenerScanCode();
}







unsigned char ucFnObtenerScanCode()
{
  unsigned char ucScanCode, val;
  unsigned int i;

  ucScanCode = inb(0x60);
  for (i=0;i<1000;i++);
  val = inb(0x61);
  for (i=0;i<1000;i++);
  outb(val|0x80,0x61);
  for (i=0;i<1000;i++);
  outb(val,0x61);

  if (iTamanioMemFijaSeteado == 0)
     {
   if(iPosicionSetMemFija < 19)
    {
      if(ucScanCode == 2) stTamanioMemFija[iPosicionSetMemFija] = '1';
             if(ucScanCode == 3) stTamanioMemFija[iPosicionSetMemFija] = '2';
      if(ucScanCode == 4) stTamanioMemFija[iPosicionSetMemFija] = '3';
             if(ucScanCode == 5) stTamanioMemFija[iPosicionSetMemFija] = '4';
      if(ucScanCode == 6) stTamanioMemFija[iPosicionSetMemFija] = '5';
             if(ucScanCode == 7) stTamanioMemFija[iPosicionSetMemFija] = '6';
      if(ucScanCode == 8) stTamanioMemFija[iPosicionSetMemFija] = '7';
             if(ucScanCode == 9) stTamanioMemFija[iPosicionSetMemFija] = '8';
      if(ucScanCode == 10) stTamanioMemFija[iPosicionSetMemFija] = '9';
             if(ucScanCode == 11) stTamanioMemFija[iPosicionSetMemFija] = '0';
             if(ucScanCode >= 2 && ucScanCode <= 11) { vFnImprimir("%c",stTamanioMemFija[iPosicionSetMemFija]); iPosicionSetMemFija++; };
    }
             if(ucScanCode == 28) iTamanioMemFijaSeteado = 1;
     }

  if (iTamanioMemFijaSeteado == 2)
     {
             if(ucScanCode == 31) { iTamanioMemFijaSeteado = 1; iModoFijoUser = 1; }
             if(ucScanCode == 49) { iTamanioMemFijaSeteado = 1; iModoFijoUser = 0; }
     }

  if (iTamanioMemFijaSeteado == 3)
     {
   if(iPosicionSetMemFija < 3)
    {
      if(ucScanCode == 2) stTamanioMemFija[iPosicionSetMemFija] = '1';
             if(ucScanCode == 3) stTamanioMemFija[iPosicionSetMemFija] = '2';
      if(ucScanCode == 4) stTamanioMemFija[iPosicionSetMemFija] = '3';
             if(ucScanCode == 5) stTamanioMemFija[iPosicionSetMemFija] = '4';
      if(ucScanCode == 6) stTamanioMemFija[iPosicionSetMemFija] = '5';
             if(ucScanCode == 7) stTamanioMemFija[iPosicionSetMemFija] = '6';
      if(ucScanCode == 8) stTamanioMemFija[iPosicionSetMemFija] = '7';
             if(ucScanCode == 9) stTamanioMemFija[iPosicionSetMemFija] = '8';
      if(ucScanCode == 10) stTamanioMemFija[iPosicionSetMemFija] = '9';
             if(ucScanCode == 11) stTamanioMemFija[iPosicionSetMemFija] = '0';
             if(ucScanCode >= 2 && ucScanCode <= 11) { vFnImprimir("%c",stTamanioMemFija[iPosicionSetMemFija]); iPosicionSetMemFija++; };
    }
             if(ucScanCode == 28) iTamanioMemFijaSeteado = 1;
     }

   if(ucScanCode == 28) iContinuar = 1;




return ucScanCode;
}
# 1035 "system.c"
void vFnHandlerTeclado ()
{
  vFnManejadorTecladoShell (ucFnObtenerScanCode());
}
# 1050 "system.c"
void vFnHandlerDisquette ()
{
  ISRDisketeraC();
}
